<?php
namespace app\index\controller;
use think\Controller;
use think\Validate;
use app\admin\model\Type;
use app\admin\model\Banner;
use app\index\model\Area;
use app\index\model\User;
use app\index\model\DeliveryAddress as Address;
use app\index\model\Collection;
use app\index\model\Order;

class Personal extends Controller
{
	// 用户个人首页
	public function index()
	{
		$user = session('user');
		if (!$user) {
			$this->redirect(url('user/login'));
		}
		$uid = $user->id;
		// 分类
    	$type = new Type();
    	$types = $type->getNormalFirstType();
    	$childType = $type->getChildType();

    	// 轮播图
    	$banner = new Banner();
    	$banners = $banner->getNormalBanner();

    	// 收藏
    	$collect = new Collection();
		$collects = $collect->where('uid',$uid)->select();
		$first_coll = $collect->where(['uid'=>$uid,'id'=>2])->find();

		// 购买订单
		$order = new Order();
		$where = [
			'uid' => $uid,
			'status' => ['neq',0],
		];
		$orders = $order->where($where)->select();
		return $this->fetch('',[
			'user' => $user,
			'types' => $types,
        	'childType' => $childType,
        	'banners' => $banners,
        	'collects' => $collects,
        	'orders' => $orders,
        	'first_coll' => $first_coll,
		]);
	}

	// 个人信息
	public function info()
	{
		$user = session('user');
		$address = $user->addrid;
		if (!empty($address)) {
			$address = explode(',', $address);
			$prov = $address[0];
			$cty = $address[1];
		} else {
			$prov = 0;
			$cty = 0;
		}
		$province = model('Area')->getProvince();
		if ($prov) {
			$city = model('Area')->getCityById($prov);
		} else {
			$city = model('Area')->getCity();
		}
		return $this->fetch('',[
			'user' => $user,
			'province' => $province,
			'city' => $city,
			'prov' => $prov,
			'cty' => $cty,
		]);
	}
	// 收货地址
	public function address()
	{
		$user = session('user');
		$uid = $user->id;
		$province = model('Area')->getProvince();
		$address = new Address();
		$addrs = $address->getAddrByUid($uid);
		$count = count($addrs);
		return $this->fetch('',[
			'user' => $user,
			'province' => $province,
			'addrs' => $addrs,
			'count' => $count,
			]);
	}

	// 编辑收货地址
	public function editAddress()
	{
		$user = session('user');
		$uid = $user->id;
		$address = new Address();	
		$addrs = $address->getAddrByUid($uid);
		$count = count($addrs);
		$id = input('get.id');
		$province = model('Area')->getProvince();
		$addr = $address->getAddrById($id);
		$provId = $addr[0]->province;
		$cityId = $addr[0]->city;
		if ($provId) {
			$city = model('Area')->getCityById($provId);
		} else {
			$city = model('Area')->getCity();
		}
		return $this->fetch('',[
			'user' => $user,
			'addr' => $addr[0],
			'province' => $province,
			'city' => $city,
			'provId' => $provId,
			'cityId' => $cityId,
			'count' => $count,
			'addrs' => $addrs,
		]);
	}

	public function saveAddr()
	{
		if (!request()->isPost()) {
			$this->error('请求不合法');
		}
		$data = input('post.');
		if (!empty($data['ck'])) {
			$data['status'] = 1;
		} else {
			$data['status'] = 0;
		}
		$address = new Address();
		$res = $address->allowField(true)->save($data,['id'=>$data['id']]);
		if ($res) {
			$this->success('编辑成功');
		} else {
			$this->error('编辑失败');
		}
	}

	// 添加收货地址
	public function saveaddress()
	{
		if (!request()->isPost()) {
			$this->error('请求不合法');
		}
		$data = input('post.');
		if (!empty($data['ck'])) {
			$data['status'] = 1;
		} else {
			$data['status'] = 0;
		}
		$address = new Address();
		$res = $address->allowField(true)->save($data);
		if ($res) {
			$this->success('新增成功');
		} else {
			$this->error('新增失败');
		}
		
	}

	// 删除收货地址
	public function deleteAddress()
	{
		$id = input('get.id');
		$address = new Address();
		$res = $address->deleteAddrById($id);
		if ($res) {
			$this->success('删除成功');
		} else {
			$this->error('删除失败');
		}
				
	}

	// 保存个人信息修改 
	public function save()
	{
		$id = session('user')->id;
		if (!request()->isPost()) {
			$this->error('请求不合法');
		}

		$data = input('post.');
		$data['addrid'] = $data['province'] . ',' . $data['city'];
		$province = model('Area')->getAddressById($data['province']);
		$city = model('Area')->getAddressById($data['city']);
		$data['address'] = $province . '-' . $city;
		$file = request()->file('icon');
		if ($file) {
			$info = $file->validate(['size'=>2000000,'exe'=>'jpg,png,gif'])->rule('uniqid')->move(ROOT_PATH.'public'.DS.'upload');
			$data['picture'] = '/upload/'.$info->getFilename();
		}
		$userModel = new User();
		$res = $userModel->allowField(true)->save($data,['id'=>$id]);
		if ($res) {
			$user = $userModel->get($id);
			session('user',$user);
			$this->success('修改成功');
		} else {
			$this->error('修改失败');
		}
	}

	public function getCityById($id)
	{
		$id = input('post.id');
		if (!$id) {
			$this->error('ID不合法');
		}
		$city = model('Area')->getCityById($id);
		if (!$city) {
			return show(0,'error');
		}
		return show(1,'success',$city);
	}

	// 我的收藏
	public function collection()
	{
		$user = session('user');
		if (!$user) {
			$this->redirect(url('user/login'));
		}
		$uid = $user->id;
		$collect = new Collection();
		$collects = $collect->where('uid',$uid)->select();
		return $this->fetch('',[
			'user' => $user,
			'collects' => $collects,
		]);
	}

	// 我的订单 
	public function order()
	{
		$user = session('user');
		if (!$user) {
			$this->redirect(url('user/login'));
		}
		$uid = $user->id;
		$orderModel = new Order();
		$order = [
			'update_time' => 'desc'
		];
		$orders = $orderModel->where('uid',$uid)->order($order)->paginate(5);
		$count = $orderModel->where('uid',$uid)->count();
		// 未完成
		$no_finish = $no_pay = $orderModel->where(['uid'=>$uid,'status'=>0])->order($order)->select();
		// 待付款
		$no_pay = $orderModel->where(['uid'=>$uid,'status'=>1])->order($order)->select();
		// 待发货
		$no_mail = $orderModel->where(['uid'=>$uid,'status'=>2])->order($order)->select();
		// 待收货
		$take_over = $orderModel->where(['uid'=>$uid,'status'=>3])->order($order)->select();
		return $this->fetch('',[
			'user' => $user,
			'orders' => $orders,
			'no_pay' => $no_pay,
			'no_mail' => $no_mail,
			'take_over' => $take_over,
			'no_finish' => $no_finish,
			'count' => $count,
		]);
	}
}